﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GeneratePlatformsScript : MonoBehaviour {

    public Transform platform, impPrefab, dungeonPrefab;

    private float platformCheck;
    private float spawnPlatformTo;

    Transform player;
    float playerHeight;

    void Start()
    {
        player = GameObject.Find("Player").transform;
        platformSpawn(-3);
        platformCheck = -3;
    }

    void Update()
    {
        playerHeight = player.position.y;

        if (playerHeight > platformCheck)
        {
            platformManager();
        }
    }

    void platformSpawn(float spawnPoint)
    {
        float y = spawnPlatformTo;

        while (y <= spawnPoint)
        {
            float x = Random.Range(2.6f, -2.6f);

            Vector3 posXYZ = new Vector3(x, y, 99);

            Transform instantiated = Instantiate(platform, posXYZ, Quaternion.identity);
            int platformFiller = Random.Range(0, 99);
            if (platformFiller <= 20)
            {
                posXYZ.y += 1.5f;
                Transform imp = Instantiate(impPrefab, posXYZ, Quaternion.identity, GameObject.Find("EnemyCollection").transform);
                imp.GetComponent<EnemyControllScript>().enemyKey = SingletonManager.GetRandomEnemy().key;
                imp.GetComponent<EnemyControllScript>().Spawn();
            }
            else if (platformFiller <= 25)
            {
                //spawn dungeon
            }

            y += Random.Range(1.4f, 3.2f);
        }

        spawnPlatformTo = spawnPoint;
    }

    void platformManager()
    {
        platformCheck = player.position.y + 4f;

        platformSpawn(platformCheck + 15);
    }
}
